﻿using System.Collections.Generic;
using System.Windows.Forms;
using CatUtils.Utils.Currency;

namespace CatUtils.Utils.SpecialUtils.View
{
    /// <summary>
    ///  界面控制辅助 注意 此方法所有函数为静态，无需new
    /// </summary>
    public class Groupboxhelper
    {
        /// <summary>
        /// 本地缓存的Groupbox，需要先进行初始化
        /// </summary>
        public static GroupBox LocalGroupBox { get; set; } = null;

        /// <summary>
        /// 默认主界面名称
        /// </summary>
        public readonly static string DefaultViewName = "MainView";

        /// <summary>
        /// 初始化主界面，自动还原上次选择
        /// </summary>
        /// <param name="groupBox"></param>
        /// <param name="comboBox"></param>
        /// <param name="ComboBoxList"></param>
        /// <param name="SelectFirst">是否选择第一个选项</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task InitAll(GroupBox groupBox, ComboBox comboBox, List<string> ComboBoxList, bool SelectFirst = true)
        {
            InitGroupBox(groupBox);
            await new ViewAsyncUpdate().ComboBoxAddItemList(comboBox, ComboBoxList);
            if (SelectFirst && comboBox.Items.Count > 0)
            {
                comboBox.SelectedIndex = 0;
                await new ViewInfoMemory().InitComboboxSaveInfo(comboBox, comboBox.Name);
            }
        }

        /// <summary>
        /// 增加一次初始化的方法
        /// </summary>
        /// <param name="groupBox"></param>
        /// <param name="comboBox"></param>
        /// <param name="ComboBoxList">选项列表</param>
        /// <param name="ViewName">读取的Combobox控件名称</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task InitAll(GroupBox groupBox, ComboBox comboBox, List<string> ComboBoxList, string ViewName)
        {
            await InitAll(groupBox, comboBox, ComboBoxList, false);
            await new ViewInfoMemory().InitComboboxSaveInfo(comboBox, ViewName);
        }

        /// <summary>
        /// 初始化界面控制
        /// </summary>
        /// <param name="groupBox"></param>
        public static void InitGroupBox(GroupBox groupBox)
        {
            LocalGroupBox = groupBox;
        }

        /// <summary>
        /// GroupBox替换操作
        /// </summary>
        /// <param name="groupBox1"></param>
        /// <param name="Control"></param>
        public static async System.Threading.Tasks.Task Replace(GroupBox groupBox1, UserControl Control)
        {
            await new ViewAsyncUpdate().GroupBoxClear(groupBox1);
            await new ViewAsyncUpdate().GroupBoxAdd(groupBox1, Control);
        }

        /// <summary>
        /// GroupBox替换操作，使用已经初始化的静态Groupbox，用Groupboxhelper.DefaultViewName设置为默认名称保存修改
        /// </summary>
        /// <param name="Control"></param>
        /// <param name="UserViewText"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task Replace(UserControl Control, string UserViewText)
        {
            await Replace(Control, UserViewText, DefaultViewName);
        }

        /// <summary>
        /// GroupBox替换操作,使用已经初始化的静态Groupbox, 并且写入保存更改
        /// </summary>
        /// <param name="Control"></param>
        /// <param name="UserViewText"></param>
        /// <param name="ViewFileName">缓存界面信息的文件名，可以调用Groupboxhelper.DefaultViewName设置为默认名称 </param>
        public static async System.Threading.Tasks.Task Replace(UserControl Control, string UserViewText, string ViewFileName)
        {
            if (LocalGroupBox == null)
            {
                PrintLog.E("未初始化本地界面模块");
                return;
            }

            await Replace(LocalGroupBox, Control, UserViewText, ViewFileName);
        }

        /// <summary>
        /// GroupBox替换操作
        /// </summary>
        /// <param name="groupBox1"></param>
        /// <param name="Control"></param>
        /// <param name="UserViewText">缓存界面信息</param>
        /// <param name="ViewFileName">缓存界面信息的文件名，可以调用Groupboxhelper.DefaultViewName设置为默认名称 </param>
        public static async System.Threading.Tasks.Task Replace(GroupBox groupBox1, UserControl Control, string UserViewText, string ViewFileName)
        {
            SaveSelectChanged(UserViewText, ViewFileName);
            await Replace(groupBox1, Control);
        }

        /// <summary>
        /// 保存选项更改 如果包含“(后台模式)”这句话，则跳过写入更改
        /// </summary>
        /// <param name="UserViewText"></param>
        /// <param name="ViewFileName">缓存界面信息的文件名，可以调用Groupboxhelper.DefaultViewName设置为默认名称 </param>
        public static void SaveSelectChanged(string UserViewText, string ViewFileName)
        {
            if (UserViewText != "")
                new ViewInfoMemory().SaveComboBoxInfo(ViewFileName, UserViewText, "(后台模式)");
        }
    }
}